package com.shxzz.shortlink.project.common.convention.exception;

import com.shxzz.shortlink.project.common.convention.errorcode.ErrorCode;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @author shxzz
 * @create 2023-12-08-19:06
 * @description 基础异常定义
 *
 * @see ClientException
 * @see RemoteException
 * @see ServiceException
 */
@Getter
public class BaseException extends RuntimeException {

    public final String errorCode;

    public final String errorMsg;

    public BaseException(String message, Throwable cause, ErrorCode errorCode) {
        super(message, cause);

        this.errorCode = errorCode.getCode();
        this.errorMsg = Optional.ofNullable(StringUtils.hasLength(message) ? message : null)
                .orElse(errorCode.getMsg());
    }

}
